package whackEm.elements;

import java.util.ArrayList;
import java.util.Random;

import whackEm.V2.WhackEmPanel;
import whackEm.persistent.GameState;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

public class BlueClock {

	private int color = 0; // 0 blue ; 1 red
	private final int BLUE = 0;
	private final int RED = 1;
	public boolean toDraw = false;	
	private Rect rect = new Rect(0,0,0,0);	
	private int drawDuration = 3000;
	private long drawStartedTime = 0;
	private int width;
	private int height;
	public int MAX_LEFT = 0;
	public int MAX_TOP = 0;
	private int timeToIncrease;
	private int timetoDecrease;
	private int blueClocklevel;
	private int redClockLevel;
	private Random r = new Random();
	private long frameTicker = 1;
	private long framePeriod;
	protected ArrayList<Bitmap> staticBlueClockBitmaps = new ArrayList<Bitmap>();
	private int currentFrame;
	private Bitmap toDrawBitmap;
	private String TAG = "Clock";
	private WhackEmPanel panel;
	private long lastClockTime = 0;
	private int clockDelay = 10000;
	private GameState gs;

	public BlueClock(int fps, float scale, WhackEmPanel panel,GameState gs) {
		this.panel = panel;
		this.gs = gs;
		blueClocklevel = gs.blueClockLevel;
		redClockLevel = gs.redClockLevel;
		timeToIncrease = (blueClocklevel * 1000);
		timetoDecrease = 6000 - (redClockLevel * 1000);
		framePeriod = 1000 / fps;
		staticBlueClockBitmaps = WhackEmPanel.staticBlueClockAnimation;
		width = staticBlueClockBitmaps.get(0).getWidth();
		height = staticBlueClockBitmaps.get(0).getHeight();
		currentFrame = 0;
		MAX_LEFT = (int) panel.convertX(480 - width - 20);
		MAX_TOP = (int) panel.convertY(293 - height - 20);
		
	}	

	public void update(long gameTime) {
		long clockTime = (System.currentTimeMillis() - lastClockTime);
		if (clockTime >= clockDelay) {
			if (r.nextInt(100) < 50 && panel.level>1){
				drawRandomClock();
			}
			lastClockTime = System.currentTimeMillis();
		}
		if (toDraw) {
			if (drawStartedTime == 0)
				drawStartedTime = gameTime;
			if (gameTime > frameTicker + framePeriod) {
				frameTicker = gameTime;
				// increment the frame
				if (currentFrame == 30)
					currentFrame = 0;
				else
					currentFrame++;
				toDrawBitmap = staticBlueClockBitmaps.get(currentFrame);
				// Log.d(TAG , "currentFrame: Update" + currentFrame);
			} else if (gameTime - drawStartedTime >= drawDuration) {
				toDraw = false;
				drawStartedTime = 0;
			}
		}

	}

	public void drawClock(Canvas canvas) {
		if (toDraw) {
			// canvas.drawBitmap(clockToDraw, pos_x, pos_y, null);
			canvas.drawBitmap(toDrawBitmap, rect.left, rect.top, null);
			// Log.d(TAG , "currentFrame: Draw" + currentFrame);
		}
	}
	
	 public void setColor() {
		    this.color = r.nextInt(2);
		    switch (BLUE) {
		    case 0:
		      staticBlueClockBitmaps = WhackEmPanel.staticBlueClockAnimation;
		      break;
		    case RED:
		      staticBlueClockBitmaps = WhackEmPanel.staticBlueClockAnimation;
		      break;
		    }
		  }
	
	private void clockTouched() {
        toDraw = false;
        switch (this.color) {
		case RED:
			panel.updateTimer(timetoDecrease, false);
			break;
		case BLUE:
			panel.updateTimer(timeToIncrease, true);
			break;		
		}
		
	}

	public void drawRandomClock() {	
		setColor();
		this.toDraw = true;
		int left = r.nextInt(MAX_LEFT - 10) + 10;
		int top = r.nextInt(MAX_TOP - 10) + 10;
		int right = left + width;
		int bottom = top + height;
		rect = new Rect(left, top, right, bottom);

	}

	public void handleActionDown(float x, float y) {
		if (x >= (rect.left) && (x <= (rect.right))) {
			if (y >= (rect.top) + 40 && (y <= (rect.bottom))) {
				if (toDraw)
					clockTouched();
			}
		}
	}

}
